import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: 'home',
    component: HomeView
  }
  ,
  {
    path: '/goods',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/Goods.vue')
  }
  ,
  {
    path: '/order',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/Order.vue')
  }
  ,
  {
    path: '/user',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/User.vue'),
    redirect:'/user/cart',
    children:[
      {
        path:'cart',
        name:'cart'
        ,
        component:()=>import('../components/user/Cart.vue')
      }
    ]
  }
  ,
  {
    path: '/free',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/Free.vue')
  },
  {
    path: '/details',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/Details.vue')
  }
  /* 404报错路由 */
  ,{
    path:'*',
    component:()=>import('../components/Error.vue')
  }

]

// const router = new VueRouter({
//   routes
// })

const router = new VueRouter({

  mode: "history",
  base: process.env.BASE_URL,
  routes,
})
router.beforeEach((to,from,next)=>{
  let token = localStorage.getItem('x-auth-token')
  if(to.path=='/user'){
    if(!token){
      alert('请登录')
      return
    }else{
      next()
    }
  }next()
})
export default router
